package com.zzyl.nursing.controller;

import cn.hutool.json.JSONObject;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备Controller
 * 
 * @author zzyl
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 从物联网平台同步产品列表
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation("从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProjectVo> projectVoList = deviceService.allProduct();
        return success(projectVoList);
    }

    /**
     * 注册设备
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return success();
    }

    /**
     * 查询设备详细数据
     * @param dto
     * @return
     */
    @PostMapping("/queryDeviceDetail")
    @ApiOperation("查询设备详细数据")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto) {
        DeviceDetailVo deviceDetailVo = deviceService.queryDeviceDetail(dto);
        return success(deviceDetailVo);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     * @param dto
     * @return
     */
    @PostMapping("/queryProductModelInfo")
    @ApiOperation("查看指定产品的已发布物模型中的功能定义详情")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto dto) {
        JSONObject jsonObject = deviceService.queryProductModelInfo(dto.getProductKey());
        return success(jsonObject);
    }

    /**
     * 查询指定设备的物模型运行状态
     * @param dto
     * @return
     */
    @PostMapping("/queryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) {
        Map<String, Object> resultMap = deviceService.queryDevicePropertyStatus(dto.getDeviceName(), dto.getProductKey());
        return success(resultMap);
    }

    /**
     * 修改设备备注名称
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改设备备注名称")
    public AjaxResult updateDeviceNickname(@RequestBody DeviceDto dto) {
        deviceService.updateDeviceNickname(dto);
        return success();
    }

    /**
     * 删除设备
     * @param dto
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除设备")
    public AjaxResult deleteDevice(@RequestBody DeviceDto dto) {
        deviceService.deleteDevice(dto.getIotId(), dto.getProductKey());
        return success();
    }

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }
}
